package UtilSever;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import cn.edu.nuc.onlinestore.model.Carts;
import cn.edu.nuc.onlinestore.model.Client;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.network.TCPClient;
import cn.edu.nuc.onlinestore.vo.Result;

public class Util {
	
	
	//用户
	
	

	public static Set<Goods> SeverSelectAllGoods()
	{
		Set<Goods> set=null;
		Result<Set<Goods>> msg=new Result(false,"findallgood",null);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return (HashSet)msg.getObj();
	}
	
	public static Result<Client> SeverSelectAClient(String name)
	{
		Result<Client> msg=new Result(false,"findaclient",name);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return msg;
		
	}
	
	//用户网络注册
	public static boolean SeverRegisterAClient(Client client)
	{
		Result<Client> msg=new Result<Client>(false,"registerclient",client);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return msg.getCode();
		
	}
	
	

	public static Goods SeverSelectAGood(int number)
	{
		Result msg=new Result(false,"selectagood",number);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return (Goods)msg.getObj();
		
	}
	
	public static  Carts SeverGetCarts(String uname){
		
		Result msg=new Result(false,"getcart",uname);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return (Carts)msg.getObj();
		
	}
	
	public static boolean SeverAddCarts(Carts car)
	{
		
		Result msg=new Result(false,"addcarts",car);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return msg.getCode();
		
	}
	//模糊查询
	public static Set<Goods> SeverSelecttheGood(String str)
	{
		
		Set<Goods> set=SeverSelectAllGoods();
		Goods g;
		Iterator<Goods> good=set.iterator();
		Set<Goods> find=new HashSet<>();
		while(good.hasNext()){
			g=good.next();
			
			int f=g.getName().indexOf(str);
			if(f!=-1)
			{
				find.add(g);
			}
		}
		
		return find;
		
	}
	
	//删除购物车
	public static boolean  SeverdeleteCarts(Client c)
	{
		
		Result<Client> msg=new Result(false,"deletecarts",c);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return msg.getCode();
	}
	
	//删除客户
	public static boolean deletheClient(Client c)
	{
		Result<Client> msg=new Result(false,"deleteUser",c);
		//获得连接
		TCPClient conn=new TCPClient();
		//发送    和    获取数据
		msg=conn.send(msg);
		return msg.getCode();
	}
	
	
}
